package jjn.carl.dp;

import java.util.Scanner;

/**
 * @author Jjn
 * @since 2023/8/22 23:43
 */
public class LeetCode122 {
    public int maxProfit(int[] prices) {
        int n = prices.length;
        // 创建二维数组存储状态
        int[][] dp = new int[n][2];
        // 初始状态
        dp[0][0] = 0;
        dp[0][1] = -prices[0];
        for (int i = 1; i < n; ++i) {
            // 第 i 天，没有股票
            dp[i][0] = Math.max(dp[i - 1][0], dp[i - 1][1] + prices[i]);
            // 第 i 天，持有股票
            dp[i][1] = Math.max(dp[i - 1][1], dp[i - 1][0] - prices[i]);
        }
        return dp[n - 1][0];
    }
    
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        int count = scanner.nextInt();
        int[] prices = new int[count];
        for (int i = 0; i < count; i++) {
            prices[i] = scanner.nextInt();
        }
        int maxProfit = new LeetCode122().maxProfit(prices);
        System.out.println(maxProfit);
    }
}
